package servlet.Person;

import com.jspsmart.upload.SmartUpload;
import service.person.PersonService;
import service.person.PersonServiceImpl;
import util.IpTimeStamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
import java.net.InetAddress;

public class threeBM extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public threeBM() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        String name = null;
        String sex = null;
        String phone = null;
        int age = 0;
        String idcard = null;
        String filename =null;
        PersonService personService = new PersonServiceImpl();
        SmartUpload smart = new SmartUpload();
            try {
                //PageContext是jsp的内置对象，在servlet不能直接使用，需要做一些处理
                JspFactory _jspxFactory = null;
                PageContext pageContext = null;
                _jspxFactory = JspFactory.getDefaultFactory();
                pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);

                smart.initialize(pageContext);//初始化上传操作
                // 设定上传限制
                // 1.限制每个上传文件的最大长度。

                // 2.限制总上传数据的长度。

                // 3.设定允许上传的文件（通过扩展名限制）,仅允许doc,txt文件。

                smart.upload();
                IpTimeStamp its = new IpTimeStamp(InetAddress.getLocalHost().getHostAddress());//request.getRemoteAddr()获得用户的ip地址
                //System.out.println("获取的ip为"+InetAddress.getLocalHost().getHostAddress());
                //如果要实现文件的批量上传，则只需用for循环，将getFile(0)中的0改为i即可
                String ext=smart.getFiles().getFile(0).getFileExt();//此为得到文件的扩展名,getFile(0)为得到唯一的一个上传文件
                String fileName="Card"+its.getIpTimeRand()+"."+ext;

                    //System.out.println("获取 的文件名为"+fileName);
                    //this.getServletContext().getRealPath("/")为得到tomcat的跟目录，放于upload文件夹中，java.io.File.separator是一种安全操作
                    //String realPath="";
                    //this.getServletContext().getRealPath("/")+
                smart.getFiles().getFile(0).saveAs(this.getServletContext().getRealPath("/")+"uploadFile"+java.io.File.separator+fileName);
                System.out.println(this.getServletContext().getRealPath("/")+"uploadFile"+java.io.File.separator+fileName);
                    filename = "uploadFile/" + fileName + "";
                //
                //由于前面的form表单已经进行了封装，这里就不能简单的用request.getparameter()来获取表单参数
                name = smart.getRequest().getParameter("name");//账号
                sex = smart.getRequest().getParameter("sex");
                phone = smart.getRequest().getParameter("phone");
                age = Integer.parseInt(smart.getRequest().getParameter("age"));
                idcard = smart.getRequest().getParameter("idcard");

            } catch (Exception e) {
                e.printStackTrace(); //code
            } finally {
                // add this section
            }
        if (filename == null) {
            response.sendRedirect(request.getContextPath() + "/end.jsp?msg=2");
        }else {
            int i = personService.threePerson(name, sex, age, phone, idcard, filename);
        if (i > 0) {

            response.sendRedirect(request.getContextPath() + "/end.jsp?msg=1");
        } else {
            response.sendRedirect(request.getContextPath() + "/end.jsp?msg=0");
        }
            }}

}
